/********************************************************************************************************
 * @file	l2cap_stack.h
 *
 * @brief	This is the header file for BLE SDK
 *
 * @author	BLE GROUP
 * @date	2020.06
 *
 * @par     Copyright (c) 2020, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *          All rights reserved.
 *
 *          Redistribution and use in source and binary forms, with or without
 *          modification, are permitted provided that the following conditions are met:
 *
 *              1. Redistributions of source code must retain the above copyright
 *              notice, this list of conditions and the following disclaimer.
 *
 *              2. Unless for usage inside a TELINK integrated circuit, redistributions
 *              in binary form must reproduce the above copyright notice, this list of
 *              conditions and the following disclaimer in the documentation and/or other
 *              materials provided with the distribution.
 *
 *              3. Neither the name of TELINK, nor the names of its contributors may be
 *              used to endorse or promote products derived from this software without
 *              specific prior written permission.
 *
 *              4. This software, with or without modification, must only be used with a
 *              TELINK integrated circuit. All other usages are subject to written permission
 *              from TELINK and different commercial license may apply.
 *
 *              5. Licensee shall be solely responsible for any claim to the extent arising out of or
 *              relating to such deletion(s), modification(s) or alteration(s).
 *
 *          THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *          ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *          WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *          DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
 *          DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *          (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *          LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *          ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *          (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *          SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************************************/
#ifndef STACK_BLE_L2CAP_STACK_H_
#define STACK_BLE_L2CAP_STACK_H_

#include "stack/ble/host/host_stack.h"

typedef struct
{
	u8 *rx_p;
	u8 *tx_p;

	u16 max_rx_size;
	u16 max_tx_size;

	u16 init_MTU;
	u16 rsvd;

	u32 mtuReqSendTimeUs;

}l2cap_buff_t;

extern l2cap_buff_t l2cap_buff_m;
extern l2cap_buff_t l2cap_buff_s;
extern l2cap_buff_t *pl2cap_buff;

void 	blt_l2cap_para_pre_init(void);

u8   	blt_UpdateParameter_request (u16 connHandle);

void 	blt_l2cap_processConnParamUpdateReq(u16 connHandle, host_acl_ms_t *);

u8*  	blt_l2cap_get_s_tx_buff(u16 connHandle);
u8*  	blt_l2cap_get_m_tx_buff(u16 connHandle);
ble_sts_t  blt_l2cap_pushData_2_controller (u16 connHandle, u16 cid, u8 *format, int format_len, u8 *pDate, int data_len);



//////////////////////////////////////////////////////////////////////////////////////////////

#if (L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_EN)

int blc_l2cap_sendCommandRejectRsp(u16 aclHandle, u8 identifier, u16 reason, u8 dataLen, u8 *pData);
int blc_l2cap_sendDisconnectReq(u16 aclHandle, u8 identifier, u16 dcid, u16 scid);
int blc_l2cap_sendDisconnectRsp(u16 aclHandle, u8 identifier, u16 dcid, u16 scid);
int blc_l2cap_sendLECreditBasedConnectReq(u16 aclHandle, u8 identifier, u16 spsm, 
	u16 scid, u16 mtu, u16 mps, u16 credits);
int blc_l2cap_sendLECreditBasedConnectRsp(u16 aclHandle, u8 identifier, u16 dcid, 
	u16 mtu, u16 mps, u16 credits, u16 result);
int blc_l2cap_sendFlowControlCreditInd(u16 aclHandle, u8 identifier, u8 cid, u8 credits);
int blc_l2cap_sendCreditBasedConnectReq(u16 aclHandle, u8 identifier, u16 spsm, u16 mtu,
	u16 mps, u16 credits, u8 numCids, u16 *pSCidList);
int blc_l2cap_sendCreditBasedConnectRsp(u16 aclHandle, u8 identifier, u16 mtu, u16 mps, 
	u16 credits, u16 result, u8 numCids, u16 *pDCidList);
int blc_l2cap_sendCreditBasedReconfigReq(u16 aclHandle, u8 identifier, u16 mtu, u16 mps,
	u8 numCids, u16 *pDCidList);
int blc_l2cap_sendCreditBasedReconfigRsp(u16 aclHandle, u8 identifier, u16 result);


l2cap_coc_handle_t *blt_l2cap_cocGetHandle(u16 aclHandle);
l2cap_coc_handle_t *blt_l2cap_cocGetIdleHandle(void);

void blt_l2cap_cocDataHandler(u16 aclHandle, rf_packet_l2cap_t *pL2cap);

bool blt_l2cap_channel_is_busy(u16 scid);
void blt_l2cap_set_channel_busy(u16 scid);
void blt_l2cap_clr_channel_busy(u16 scid);

u8 blt_l2cap_getIdleIdentifier(void);
u8 blt_l2cap_cocGetSrvNum(l2cap_coc_service_t *pServer);
l2cap_coc_service_t *blt_l2cap_cocGetService(u8 srvNum);
l2cap_coc_service_t *blt_l2cap_cocGetIdleService(u16 spsm);
l2cap_coc_service_t *blt_l2cap_cocSearchService(u16 aclHandle, u8 identifier);
l2cap_coc_service_t *blt_l2cap_cocSearchPendingService(u16 aclHandle, u8 identifier);
l2cap_coc_service_t *blt_l2cap_cocSearchDisconnService(u16 aclHandle, u8 identifier);
l2cap_coc_service_t *blt_l2cap_cocSearchServiceBySpsm(u16 aclHandle, u16 spsm);
l2cap_coc_service_t *blt_l2cap_cocSearchServiceByScid(u16 aclHandle, u16 scid, u16 *pDcid);
l2cap_coc_service_t *blt_l2cap_cocSearchServiceByDcid(u16 aclHandle, u16 dcid, u16 *pScid);
int  blt_l2cap_cocMallocService(u16 spsm, u8 chCount, blc_l2cap_coc_evtcb evtCb, 
	 blc_l2cap_coc_datacb dataCb);
void blt_l2cap_cocFreeService(u16 servNum);
void blt_l2cap_cocFreeService1(l2cap_coc_service_t *pServer);

l2cap_coc_channel_t *blt_l2cap_cocGetChannel(u16 scid);
l2cap_coc_channel_t *blt_l2cap_cocSearchChByDcid(u16 aclHandle, u16 dcid, u16 *pScid);
u8 blt_l2cap_cocMallocChannel(u16 chCount, u16 *pChList);
void blt_l2cap_cocFreeChannel(u16 chCount, u16 *pCidList);

u8 *blt_signal_l2capCmdHandler(u16 connHandle, u8 * p);

#endif

#endif /* L2CAP_STACK_H_ */
